package com.ttmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ttmall.product.entity.AttrEntity;
import com.ttmall.product.service.AttrAttrgroupRelationService;
import com.ttmall.product.service.AttrService;
import com.ttmall.product.service.CategoryService;
import com.ttmall.product.vo.AttrGroupRelationVO;
import com.ttmall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ttmall.product.entity.AttrGroupEntity;
import com.ttmall.product.service.AttrGroupService;
import com.ttmall.common.utils.PageUtils;
import com.ttmall.common.utils.R;



/**
 * 属性分组
 *
 * @author xxx
 * @email xxx@gmail.com
 * @date 2021-11-27 21:49:05
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;


    // 新增关联关系
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> vos){

        attrAttrgroupRelationService.saveBatch(vos);

        return R.ok();
    }

    //product/attrgroup/1/attr/relation

    @GetMapping("/{attrgroupId}/attr/relation")
    public  R  attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data" , list);
    }

    @GetMapping("/{catelogId}/withattr")
    public  R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        // 1. 查出当前分类下的所有属性分组
        //2. 查出每个分组的所有属性
        List<AttrGroupWithAttrsVO> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return  R.ok().put("data",vos);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public  R  attrNoRelation(@RequestParam Map<String, Object> params ,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page" , page);
    }

    @PostMapping("/attr/relation/delete")
    public  R deleteRelation(@RequestBody AttrGroupRelationVO[] vos){
        attrService.deleteRation(vos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params ,
                  @PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long [] path =categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
